package com.manager.commons;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * Copyrights 1997-2010 西安协同时光软件有限公司
 * 
 * Projectname: jinduicheng
 * 
 * Filename:RandomKey.java
 * 
 * Description: 生成随机主键
 * 
 * Version:V0.1
 * 
 * History: 
 * [  Author 	     Date 	      Version	    Content ] 
 *   shiyu 	   	    
 * 
 */
public class RandomKey {
	
	/**
	 * 返回给定长度的随机数
	 * 
	 * @param len
	 * @return
	 */
	public static String random(int len) {
		String rd = random();
		if (len < rd.length())
			rd = rd.substring(0, len);
		else
			rd = rd.substring(0);
		return rd;
	}

	public static String random() {
		double rd = Math.random();
		return String.valueOf(rd).substring(2);
	} 

	
	synchronized public static String generateID_String_19() {
		String id = getCurrTimeMillisecond() + random(5);
		return id;
	}
	
	synchronized public static String generateID_Long_19() {
		String id = getCurrTimeMillisecond() + random(5);
		return id;
	}
	
	synchronized public static String generateID_String_19_for(int i) {
		String id = getCurrTimeMillisecond()+ i + random(4);
		return id;
	}
	
	public static String getCurrTimeMillisecond() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHssmm");
		String s = outFormat.format(now);
		return s;
	}
	 

}
